from typing import Set
from ..data_structures.base_structures import *
import shapefile
import os

def visualize_open_set(open_set: Set):
    shp_path = 'output/debug/open_set.shp'
    
    # 确保输出目录存在
    os.makedirs(os.path.dirname(shp_path), exist_ok=True)
    
    with shapefile.Writer(shp_path) as shp:
        # 定义字段结构
        shp.field('ID', 'N', 10, 0)  # 添加一个ID字段
        shp.field('CELL_TYPE', 'C', 50)  # 添加单元格类型字段
        
        for i, cell in enumerate(open_set):
            cell: BaseCell
            # 添加几何图形
            shp.poly([[(lon, lat) for lat, lon in cell.vertices]])
            # 添加对应的属性记录
            shp.record(i + 1, type(cell).__name__)

    # 为 Shapefile 文件生成 WGS84 坐标系的 .prj 文件
    wgs84_prj = """GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563]],
        PRIMEM["Greenwich",0],
        UNIT["degree",0.0174532925199433]]"""
    prj_path = os.path.splitext(shp_path)[0] + ".prj"
    with open(prj_path, "w") as prj_file:
        prj_file.write(wgs84_prj)